/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Controle.java
 *
 * Created on Oct 16, 2009, 12:21:29 AM
 */
package simulador;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.io.*;
import java.util.Iterator;
import javax.swing.*;

/**
 *
 * @author rangel
 */
public class Controle extends javax.swing.JFrame {

	private Options options;
	private Thread mainLoop;
	private Goal agent;
	private About about = null;
	Ambientes ambientes = null;
	boolean potentialFields = true;
	boolean chase = false;
	boolean astar = false;
	final private MyFileChooser mapSelector = new MyFileChooser();

	/** Creates new form Controle */
	public Controle() {
		initComponents();
		ambientes = new Ambientes(this, true);
		options = new Options(this, rootPaneCheckingEnabled);
		scenario1.requestFocus();
		scenario1.requestFocusInWindow();
		scenario1.setParent(this);

		/*
		 * Ajusta LookAndFeel
		 */
		try {
			UIManager.LookAndFeelInfo[] looks = UIManager.getInstalledLookAndFeels();
			UIManager.setLookAndFeel(looks[looks.length - 1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setLifeTExt(String t)
	{
		jLabelLife.setText(t);
	}

	public void setMoneyTExt(String t)
	{
		jLabelMoney.setText(t);
	}
	
	public void setLvlText(String t)
	{
		labelLevel.setText(t);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scenario1 = new simulador.Scenario();
        jToolBar2 = new javax.swing.JToolBar();
        buttonTorre = new javax.swing.JToggleButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        jLabelLife = new javax.swing.JLabel();
        jLabelMoney = new javax.swing.JLabel();
        labelLevel = new javax.swing.JLabel();
        jSeparator5 = new javax.swing.JToolBar.Separator();
        heartButton = new javax.swing.JToggleButton();
        jToolBar1 = new javax.swing.JToolBar();
        buttonBot = new javax.swing.JToggleButton();
        buttonInsertAgent = new javax.swing.JButton();
        buttonGoal = new javax.swing.JToggleButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        buttonObstacle = new javax.swing.JToggleButton();
        buttonLine = new javax.swing.JToggleButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        buttonPickColor = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        buttonStart = new javax.swing.JButton();
        buttonStop = new javax.swing.JButton();
        statusBar = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jMenuItem15 = new javax.swing.JMenuItem();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        menuItemOptions1 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu8 = new javax.swing.JMenu();
        jMenuItem18 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Gekkehond");
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });

        scenario1.setOpaque(false);

        jToolBar2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jToolBar2.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar2.setRollover(true);

        buttonTorre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icoYoshi.png"))); // NOI18N
        buttonTorre.setToolTipText("Insert Yoshi's Tower");
        buttonTorre.setEnabled(false);
        buttonTorre.setFocusable(false);
        buttonTorre.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonTorre.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonTorre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTorreActionPerformed(evt);
            }
        });
        jToolBar2.add(buttonTorre);
        jToolBar2.add(jSeparator4);

        jLabelLife.setText("♥10");
        jToolBar2.add(jLabelLife);

        jLabelMoney.setText("$100");
        jToolBar2.add(jLabelMoney);

        labelLevel.setText("Lvl 1");
        jToolBar2.add(labelLevel);
        jToolBar2.add(jSeparator5);

        heartButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/heartIco.gif"))); // NOI18N
        heartButton.setToolTipText("Centro de Recuperação dos Monstros");
        heartButton.setEnabled(false);
        heartButton.setFocusable(false);
        heartButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        heartButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        heartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                heartButtonActionPerformed(evt);
            }
        });
        jToolBar2.add(heartButton);

        javax.swing.GroupLayout scenario1Layout = new javax.swing.GroupLayout(scenario1);
        scenario1.setLayout(scenario1Layout);
        scenario1Layout.setHorizontalGroup(
            scenario1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(scenario1Layout.createSequentialGroup()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(825, Short.MAX_VALUE))
        );
        scenario1Layout.setVerticalGroup(
            scenario1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
        );

        getContentPane().add(scenario1, java.awt.BorderLayout.CENTER);

        jToolBar1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jToolBar1.setRollover(true);

        buttonBot.setText("Insert Bot");
        buttonBot.setFocusable(false);
        buttonBot.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonBot.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonBot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonBotActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonBot);

        buttonInsertAgent.setText("Insert Agent");
        buttonInsertAgent.setEnabled(false);
        buttonInsertAgent.setFocusable(false);
        buttonInsertAgent.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonInsertAgent.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonInsertAgent.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonInsertAgentActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonInsertAgent);

        buttonGoal.setText("Insert Goal");
        buttonGoal.setFocusable(false);
        buttonGoal.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonGoal.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonGoal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonGoalActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonGoal);
        jToolBar1.add(jSeparator2);

        buttonObstacle.setText("Insert Obstacle");
        buttonObstacle.setFocusable(false);
        buttonObstacle.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonObstacle.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonObstacle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonObstacleActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonObstacle);

        buttonLine.setText("Insert line");
        buttonLine.setFocusable(false);
        buttonLine.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonLine.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonLineActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonLine);
        jToolBar1.add(jSeparator1);

        buttonPickColor.setText("Pick Color (A*)");
        buttonPickColor.setEnabled(false);
        buttonPickColor.setFocusable(false);
        buttonPickColor.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonPickColor.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonPickColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonPickColorActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonPickColor);
        jToolBar1.add(jSeparator3);

        buttonStart.setText("Start");
        buttonStart.setFocusable(false);
        buttonStart.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonStart.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStartActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonStart);

        buttonStop.setText("Stop");
        buttonStop.setEnabled(false);
        buttonStop.setFocusable(false);
        buttonStop.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        buttonStop.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        buttonStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonStopActionPerformed(evt);
            }
        });
        jToolBar1.add(buttonStop);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        statusBar.setText("Pontential fields selected!");
        statusBar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        getContentPane().add(statusBar, java.awt.BorderLayout.PAGE_END);

        jMenu5.setText("File");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setText("New Potential Fields");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem2);

        jMenuItem11.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem11.setText("Chase");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem11);

        jMenu6.setText("A*");

        jMenuItem12.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem12.setText("Start A* mode");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem12);

        jMenuItem8.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem8.setText("Save map");
        jMenuItem8.setEnabled(false);
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem8);

        jMenuItem9.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem9.setText("Open map");
        jMenuItem9.setEnabled(false);
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem9);

        jMenu5.add(jMenu6);

        jMenuItem15.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem15.setText("Restart");
        jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem15);

        jMenuItem16.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        jMenuItem16.setText(" Exit");
        jMenuItem16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem16);

        jMenuBar1.add(jMenu5);

        jMenu7.setText("Tools");

        menuItemOptions1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        menuItemOptions1.setText("Preferences");
        menuItemOptions1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemOptionsActionPerformed(evt);
            }
        });
        jMenu7.add(menuItemOptions1);

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Environments");
        jMenuItem6.setEnabled(false);
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem6);

        jMenuBar1.add(jMenu7);

        jMenu8.setText("Help");

        jMenuItem18.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem18.setText("About");
        jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu8.add(jMenuItem18);

        jMenuBar1.add(jMenu8);

        setJMenuBar(jMenuBar1);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-889)/2, (screenSize.height-722)/2, 889, 722);
    }// </editor-fold>//GEN-END:initComponents

	private void buttonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopActionPerformed
		buttonStop.setEnabled(false);
		buttonStart.setEnabled(true);
		if (astar) {
			buttonPickColor.setEnabled(true);
			buttonObstacle.setEnabled(true);
			buttonGoal.setEnabled(true);
			buttonBot.setEnabled(true);
			heartButton.setEnabled(true);
		}
		scenario1.setIsRunning(false);
		mainLoop.stop();
	}//GEN-LAST:event_buttonStopActionPerformed

	private void buttonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStartActionPerformed
		// TODO add your handling code here:
		if (mainLoop == null) {
			mainLoop = new Thread(scenario1);
			mainLoop.start();
			scenario1.setIsRunning(true);
		} else {
			scenario1.setIsRunning(true);
		}
		if (astar) {
			if (scenario1.enemiesToGo <= 0) {
				scenario1.enemiesToGo = 10;
			}
			scenario1.planejaCaminho();
			buttonObstacle.setEnabled(false);
			buttonObstacle.setSelected(false);
			buttonGoal.setEnabled(false);
			buttonGoal.setSelected(false);
			buttonBot.setEnabled(false);
			buttonBot.setSelected(false);
			buttonPickColor.setEnabled(false);
			scenario1.setInsertingGoal(false);
			scenario1.setInsertingBot(false);
			scenario1.setInsertingTorre(true);
			buttonTorre.setSelected(true);
		}
		buttonStop.setEnabled(true);
		buttonStart.setEnabled(false);
		heartButton.setEnabled(false);
		
	}//GEN-LAST:event_buttonStartActionPerformed

	private void menuItemOptionsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemOptionsActionPerformed
		// TODO add your handling code here:
		options.setScenario(scenario1);
		options.setVisible(true);
	}//GEN-LAST:event_menuItemOptionsActionPerformed

	private void buttonGoalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGoalActionPerformed
		// TODO add your handling code here:
		scenario1.setInsertingGoal(buttonGoal.isSelected());
		if (buttonLine.isSelected()) {
			buttonLine.setSelected(false);
			scenario1.setInsertingLine(false);
		}
		
		scenario1.setInsertingHeart(false);
		heartButton.setSelected(false);
		
		if (buttonObstacle.isSelected()) {
			buttonObstacle.setSelected(false);
			scenario1.setInsertingObstacle(false);
		}
		if (buttonBot.isSelected()) {
			buttonBot.setSelected(false);
			scenario1.setInsertingBot(false);
		}
		if (buttonTorre.isSelected()) {
			buttonTorre.setSelected(false);
			scenario1.setInsertingTorre(false);
		}
		scenario1.setInsertingHeart(false);
	heartButton.setSelected(false);
	}//GEN-LAST:event_buttonGoalActionPerformed

	private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
		// TODO add your handling code here:
		if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
			System.exit(0);
		}
		if (agent != null) {
			switch (evt.getKeyCode()) {
				case KeyEvent.VK_DOWN:
					agent.motoresAtivos[0] = 'd';
					break;
				case KeyEvent.VK_UP:
					agent.motoresAtivos[0] = 'u';
					break;
				case KeyEvent.VK_RIGHT:
					agent.motoresAtivos[1] = 'r';
					break;
				case KeyEvent.VK_LEFT:
					agent.motoresAtivos[1] = 'l';
					break;
			}
		}
		scenario1.repaint();

	}//GEN-LAST:event_formKeyPressed

	private void buttonInsertAgentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonInsertAgentActionPerformed
		// TODO add your handling code here:
		agent = new Goal(new Point(50, 50), 30, 30, "agent");
		scenario1.addAgent(agent);
		scenario1.setMovingAgent(true);
		if(!scenario1.isRunning) scenario1.repaint();
	}//GEN-LAST:event_buttonInsertAgentActionPerformed

	private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
		// TODO add your handling code here:
		for (int i = 0; agent != null && i < agent.motoresAtivos.length; i++) {
			agent.motoresAtivos[i] = 0;
		}
	}//GEN-LAST:event_formKeyReleased

	private void buttonBotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonBotActionPerformed
		// TODO add your handling code here:
		scenario1.setInsertingBot(buttonBot.isSelected());

		buttonGoal.setSelected(false);
		scenario1.setInsertingGoal(false);

		scenario1.setMovingAgent(false);

		buttonLine.setSelected(false);
		scenario1.setInsertingLine(false);

		buttonObstacle.setSelected(false);
		scenario1.setInsertingObstacle(false);

		scenario1.setInsertingHeart(false);
		heartButton.setSelected(false);
		
		buttonTorre.setSelected(false);
		scenario1.setInsertingTorre(false);

		scenario1.setInsertingHeart(false);
	heartButton.setSelected(false);

	}//GEN-LAST:event_buttonBotActionPerformed

	private void buttonObstacleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonObstacleActionPerformed
		// TODO add your handling code here:
		scenario1.setInsertingObstacle(buttonObstacle.isSelected());
		if (buttonGoal.isSelected()) {
			buttonGoal.setSelected(false);
			scenario1.setInsertingGoal(false);
		}
		if (buttonBot.isSelected()) {
			buttonBot.setSelected(false);
			scenario1.setInsertingBot(false);
		}
		if (buttonLine.isSelected()) {
			buttonLine.setSelected(false);
			scenario1.setInsertingLine(false);
		}
		scenario1.setMovingAgent(false);
		buttonTorre.setSelected(false);
		scenario1.setInsertingTorre(false);
		heartButton.setSelected(false);
		scenario1.setInsertingHeart(false);
	}//GEN-LAST:event_buttonObstacleActionPerformed

	private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
		// TODO add your handling code here:
		statusBar.setText("Pontential fields selected!");
		potentialFields = true;
		chase = false;
		astar = false;
		scenario1.setAlgAstar(astar);
		scenario1.setPotentialFields(potentialFields);
		scenario1.setChase(chase);
		scenario1.setAtractionBots(true);
		scenario1.setMovingAgent(false);
		buttonInsertAgent.setEnabled(false);
		buttonPickColor.setEnabled(false);
		buttonGoal.setEnabled(true);
		heartButton.setEnabled(false);
		buttonObstacle.setEnabled(true);
		buttonTorre.setEnabled(false);
		scenario1.repaint();
	}//GEN-LAST:event_jMenuItem2ActionPerformed

private void buttonLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonLineActionPerformed
// TODO add your handling code here:
	scenario1.setInsertingLine(buttonLine.isSelected());
	if (buttonGoal.isSelected()) {
		buttonGoal.setSelected(false);
		scenario1.setInsertingGoal(false);
	}
	if (buttonObstacle.isSelected()) {
		buttonObstacle.setSelected(false);
		scenario1.setInsertingObstacle(false);
	}
	if (buttonBot.isSelected()) {
		buttonBot.setSelected(false);
		scenario1.setInsertingBot(false);
	}
	scenario1.setMovingAgent(false);
	buttonTorre.setSelected(false);
	scenario1.setInsertingTorre(false);
}//GEN-LAST:event_buttonLineActionPerformed

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
	// TODO add your handling code here:
	statusBar.setText("Chase Evade selected!");
	chase = true;
	potentialFields = false;
	astar = false;
	scenario1.setAlgAstar(astar);
	scenario1.setPotentialFields(potentialFields);
	scenario1.setChase(chase);
	scenario1.setAtractionBots(false);
	scenario1.setModifyingGrid(false);
	buttonGoal.setEnabled(false);
	scenario1.setMovingAgent(false);
	buttonPickColor.setEnabled(false);
	buttonInsertAgent.setEnabled(true);
	buttonObstacle.setEnabled(true);
	buttonTorre.setEnabled(false);
	buttonTorre.setSelected(false);
}//GEN-LAST:event_jMenuItem3ActionPerformed

private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
	// TODO add your handling code here:
	System.exit(0);
}//GEN-LAST:event_jMenuItem4ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
	if (about == null) {
		about = new About(this, true);
	}
	about.setVisible(true);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
	// TODO add your handling code here:
	scenario1.newSimulation();
	jMenuItem2.doClick();
	scenario1.setMyLife(10);
	scenario1.setMyMoney(100);
	scenario1.setInsertingTorre(false);

	buttonTorre.setEnabled(false);
	buttonTorre.setSelected(false);

	heartButton.setSelected(false);
	scenario1.setInsertingHeart(false);
}//GEN-LAST:event_jMenuItem5ActionPerformed

private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
	// TODO add your handling code here:
	if (astar) {
		ambientes.setVisible(true);
		if (ambientes.getColor() != null && ambientes.getCost() >= 0) {
			scenario1.setColorGrid(ambientes.getColor());
			scenario1.setCostAtom(ambientes.getCost());
		}
	}
}//GEN-LAST:event_jMenuItem6ActionPerformed

private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
	scenario1.newSimulation();
	astar = true;
	chase = false;
	potentialFields = false;
	scenario1.setAtractionBots(false);
	scenario1.setAlgAstar(true);
	scenario1.setPotentialFields(false);
	scenario1.setChase(false);
	buttonInsertAgent.setEnabled(false);
	buttonGoal.setSelected(false);
	buttonLine.setEnabled(false);
	buttonInsertAgent.setEnabled(false);
	jMenuItem6.setEnabled(true);
	buttonTorre.setEnabled(true);
	scenario1.setInsertingGoal(false);
	scenario1.setInsertingBot(false);
	scenario1.setInsertingTorre(false);
	scenario1.setModifyingGrid(true);
	heartButton.setEnabled(true);
	buttonPickColor.setEnabled(true);
	jMenuItem9.setEnabled(true);
	statusBar.setText("Simulating A* algorithm");
	buttonObstacle.setEnabled(false);
}//GEN-LAST:event_jMenuItem7ActionPerformed

private void buttonPickColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPickColorActionPerformed
	jMenuItem6.doClick();
	if (buttonBot.isSelected()) {
		buttonBot.doClick();
	}
	if (buttonGoal.isSelected()) {
		buttonGoal.doClick();
	}
	if (buttonObstacle.isSelected()) {
		buttonObstacle.doClick();
	}
	buttonTorre.setSelected(false);
	scenario1.setInsertingTorre(false);
	heartButton.setSelected(false);
	scenario1.setInsertingHeart(false);
}//GEN-LAST:event_buttonPickColorActionPerformed

private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
	if (scenario1.algAstar) {
		mapSelector.showSaveDialog(this);
		try {
			FileWriter escritor = new FileWriter(mapSelector.getSelectedFile());

			escritor.write(getWidth() + " " + getHeight() + "\n");
			escritor.write((int) scenario1.currentBot.xPosition + " " + (int) scenario1.currentBot.yPosition + "\n");
			escritor.write((int) scenario1.currentGoal.xPosition + " " + (int) scenario1.currentGoal.yPosition + "\n");
			// Guarda a posicao do coracao
			escritor.write(scenario1.heartPosition.x + " " + scenario1.heartPosition.y + "\n");
			for (int i = 0; i < scenario1.grid.size(); i++) {
				Atom aux = scenario1.grid.get(i);
				Color color = aux.getColor();
				float cost = aux.getCost();
				escritor.write(aux.getX() + " " + aux.getY() + " " + color.getRGB() + " " + cost + "\n");
			}
			escritor.close();

		} catch (IOException e) {
			// I don't care
		}
	}
}//GEN-LAST:event_jMenuItem8ActionPerformed

private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed
	int result = mapSelector.showOpenDialog(this);
	if(result!=mapSelector.APPROVE_OPTION)
		return;

	jMenuItem15.doClick();
	jMenuItem12.doClick();
	
	try {
		FileInputStream arquivo = new FileInputStream(mapSelector.getSelectedFile());
		java.util.Scanner din = new java.util.Scanner(arquivo);

		int w, h;
		w = din.nextInt();
		h = din.nextInt();

		setSize(w, h);
		if (scenario1.grid != null) {
			scenario1.grid.clear();
		}

		scenario1.grid = new java.util.ArrayList<Atom>();

		// Le coordenadas do starting point
		int x, y;
		x = din.nextInt();
		y = din.nextInt();
		if (scenario1.currentBot != null) {
			scenario1.currentBot.move(x, y);
		} else {
			if (scenario1.currentGoal != null && x >= scenario1.currentGoal.xPosition) {
				scenario1.currentBot = new Bot(new Point(x,y), 35, 35, "bot", scenario1.getAtractionBots(), scenario1, 10, "/images/Tartaruga-avoadoreE.gif");
			} else {
				scenario1.currentBot = new Bot(new Point(x,y), 35, 35, "bot", scenario1.getAtractionBots(), scenario1, 10, "/images/Tartaruga-avoadoreD.gif");
			}
		}

		// Le coordenadas do goal
		x = din.nextInt();
		y = din.nextInt();
		if (scenario1.currentGoal != null) {
			scenario1.currentGoal.updatePosition(new Point(x, y));
		} else {
			scenario1.currentGoal = new Goal(new Point(x, y), 35, 35, "goal", scenario1.cellSpace,scenario1,"/images/Peach.gif");
		}
		
		// Le coordenadas do coracao
		x = din.nextInt();
		y = din.nextInt();
		if (scenario1.heartPosition != null) {
			scenario1.heartPosition.move(x, y);
		} else {
			scenario1.heartPosition = new Heart(x, y, scenario1);
		}

		while (din.hasNext()) {
			Point p = new Point(din.nextInt(), din.nextInt());
			Color c = new Color(din.nextInt());

			Atom auxA = new Atom(c, p, Float.parseFloat(din.next()));
			scenario1.grid.add(auxA);
		}
		din.close();
		arquivo.close();
		jMenuItem8.setEnabled(true);
		buttonStart.setEnabled(true);
		scenario1.repaint();

	} catch (IOException e) {
		// I don't care
	}
}//GEN-LAST:event_jMenuItem9ActionPerformed

private void buttonTorreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTorreActionPerformed
	// TODO add your handling code here:
	scenario1.setInsertingBot(false);
	buttonBot.setSelected(false);
	scenario1.setInsertingEnemy(false);
	scenario1.setInsertingGoal(false);
	buttonGoal.setSelected(false);
	scenario1.setInsertingLine(false);
	buttonLine.setSelected(false);
	scenario1.setInsertingObstacle(false);
	buttonObstacle.setSelected(false);
	scenario1.setInsertingHeart(false);
	heartButton.setSelected(false);
	scenario1.setInsertingTorre(buttonTorre.isSelected());
}//GEN-LAST:event_buttonTorreActionPerformed

private void heartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_heartButtonActionPerformed
	// TODO add your handling code here:
	scenario1.setInsertingBot(false);
	buttonBot.setSelected(false);
	scenario1.setInsertingEnemy(false);
	scenario1.setInsertingGoal(false);
	buttonGoal.setSelected(false);
	scenario1.setInsertingLine(false);
	buttonLine.setSelected(false);
	scenario1.setInsertingObstacle(false);
	buttonObstacle.setSelected(false);
	scenario1.setInsertingTorre(false);
	buttonTorre.setSelected(false);
	scenario1.setInsertingHeart(true);
	scenario1.setInsertingBot(false);
	buttonBot.setSelected(false);
	scenario1.setInsertingEnemy(false);
	scenario1.setInsertingGoal(false);
	buttonGoal.setSelected(false);
	scenario1.setInsertingLine(false);
	buttonLine.setSelected(false);
	scenario1.setInsertingObstacle(false);
	buttonObstacle.setSelected(false);
	scenario1.setInsertingTorre(false);
	buttonTorre.setSelected(false);
}//GEN-LAST:event_heartButtonActionPerformed

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new Controle().setVisible(true);
			}
		});
	}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton buttonBot;
    private javax.swing.JToggleButton buttonGoal;
    private javax.swing.JButton buttonInsertAgent;
    private javax.swing.JToggleButton buttonLine;
    private javax.swing.JToggleButton buttonObstacle;
    private javax.swing.JButton buttonPickColor;
    private javax.swing.JButton buttonStart;
    private javax.swing.JButton buttonStop;
    private javax.swing.JToggleButton buttonTorre;
    private javax.swing.JToggleButton heartButton;
    private javax.swing.JLabel jLabelLife;
    private javax.swing.JLabel jLabelMoney;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenu jMenu8;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem18;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar.Separator jSeparator5;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JLabel labelLevel;
    private javax.swing.JMenuItem menuItemOptions1;
    private simulador.Scenario scenario1;
    private javax.swing.JLabel statusBar;
    // End of variables declaration//GEN-END:variables

	public void stopSimulation() {
		buttonStop.doClick();
	}

	public javax.swing.JButton buttonStart() {
		return this.buttonStart;
	}

	public javax.swing.JMenuItem menuItemSave() {
		return this.jMenuItem8;
	}
}
